package com.robot.goods.controller;

import com.alibaba.fastjson.JSON;
import com.robot.goods.pojo.Goods;
import com.robot.goods.service.GoodsService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * goods控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsJsonController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "third/findGoodsBySearch/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Goods> pageList = goodsService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<Goods>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过categoryId查询goods列表并分页
     *
     * @param categoryId
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findGoodsByCategoryId/{categoryId}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsByCategoryId(@PathVariable String categoryId, @PathVariable int page, @PathVariable int size) {
        Page<Goods> pageList = goodsService.findAllByCategoryIdAndDeletedAtIsNullOrderByCreatedAtDesc(categoryId, page, size);
        return JSON.toJSONString(new PageResult<Goods>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过categoryId和storeId查询goods列表并分页
     *
     * @param categoryId
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findGoodsByCategoryIdAndStoreId/{categoryId}/{storeId}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsByCategoryIdAndStoreId(@PathVariable String categoryId, @PathVariable String storeId, @PathVariable int page, @PathVariable int size) {
        Page<Goods> pageList = goodsService.findAllByCategoryIdAndStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(categoryId, storeId, page, size);
        return JSON.toJSONString(new PageResult<Goods>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过storeId查询goods列表并分页
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findGoodsByStoreId/{storeId}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsByStoreId(@PathVariable String storeId, @PathVariable int page, @PathVariable int size) {
        Page<Goods> pageList = goodsService.findAllByStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(storeId, page, size);
        return JSON.toJSONString(new PageResult<Goods>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过categoryId,onSale,status查询goods列表并分页
     *
     * @param categoryId
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findGoodsByCategoryIdAndOnSaleAndStatus/{categoryId}/{onSale}/{status}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsByCategoryIdAndOnSaleAndStatus(@PathVariable String categoryId, @PathVariable String onSale, @PathVariable String status, @PathVariable int page, @PathVariable int size) {
        Page<Goods> pageList = goodsService.findAllByCategoryIdAndOnSaleAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(categoryId, onSale, status, page, size);
        return JSON.toJSONString(new PageResult<Goods>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过storeId,onSale,status查询goods列表并分页
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findGoodsByStoreIdAndOnSaleAndStatus/{storeId}/{onSale}/{status}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsByStoreIdAndOnSaleAndStatus(@PathVariable String storeId, @PathVariable String onSale, @PathVariable String status, @PathVariable int page, @PathVariable int size) {
        Page<Goods> pageList = goodsService.findAllByStoreIdAndOnSaleAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(storeId, onSale, status, page, size);
        return JSON.toJSONString(new PageResult<Goods>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过id和storeId查询goods
     *
     * @param id
     * @param storeId
     * @return
     */
    @RequestMapping(value = "third/findGoodsByIdAndStoreId/{id}/{storeId}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsByIdAndStoreId(@PathVariable String id, @PathVariable String storeId) {
        return JSON.toJSONString(goodsService.findByIdAndStoreIdAndDeletedAtIsNull(id, storeId));
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "third/findGoodsById/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindGoodsById(@PathVariable String id) {
        return JSON.toJSONString(goodsService.findById(id));
    }

    /**
     * 增加
     *
     * @param goods
     */
    @RequestMapping(value = "third/addGoodsByGoods", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public void thirdAddGoodsByGoods(@RequestBody String goods) {
        goodsService.add(JSON.parseObject(goods, Goods.class));
    }

    /**
     * 修改
     *
     * @param goods
     */
    @RequestMapping(value = "third/updateGoodsByGoods", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public void thirdUpdateGoodsByGoods(@RequestBody String goods) {
        goodsService.update(JSON.parseObject(goods, Goods.class));
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "third/deleteGoodsById/{id}", method = RequestMethod.DELETE, produces = "application/json; charset=utf-8")
    public void thirdDeleteGoodsById(@PathVariable String id) {
        goodsService.deleteById(id);
    }
}
